/*
 * @Author: luosi 1297957337@qq.com
 * @Date: 2024-12-02 15:28:31
 * @LastEditors: luosi 1297957337@qq.com
 * @LastEditTime: 2024-12-05 13:53:54
 * @FilePath: \sr_system_manager_ui\src\utils\dict.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import useDictStore from '@/store/modules/dict'
import { dictionaryListApi } from '@/api/system/dict';
import { dictEnum } from './constEnum';
import { getDepartmentListReq } from '@/api/system/user';


/**
 * 获取字典数据
 */
export function useDict(...args) {
  const res = ref({});
  return (() => {
    args.forEach((dictType) => {
      res.value[dictType] = [];
      const dicts = useDictStore().getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        // 所属院系特殊处理
        if(dictType === dictEnum.department) {
          getDepartmentListReq().then(resp => {
            res.value[dictType] = resp?.map(p => ({ title: p.departmentName, value: p.departmentId }))
            useDictStore().setDict(dictType, res.value[dictType]);
          })
        } else {
          dictionaryListApi({code: [dictType] }).then(resp => {
            res.value[dictType] = resp?.[0].listDataRespVOList?.map(p => ({ title: p.dataName, value: p.dataId, remark: p.dataRemark, order: p.order }))
            useDictStore().setDict(dictType, res.value[dictType]);
          })
        }
      }
    })
    return res.value;
  })()
}